package com.fan.gofast.util;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName RegexUtil
 * @Decription
 * @Author wangjing
 * @Date 23.3.8 0008 14:39
 **/
public class RegexUtil {
    public static final String EMOJI_REGEX = "[^\\u0000-\\uFFFF]";
    public static final String EMAIL_REGEX = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
    public static final String IDCARD_REGEX = "(^[1-9]\\d{5}([12][0-9])\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)";
    public static final String MOBILE_REGEX = "(\\+\\d+)?1\\d{10}$";
    public static final String PHONE_REGEX = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
    public static final String DIGIT_REGEX = "\\-?\\d+";
    public static final String DECIMALS_REGEX = "\\-?\\d+(\\.\\d+)?";
    public static final String BLANKSPACE_REGEX = "\\s+";
    public static final String CHINESE_REGEX = "^[一-龥]+$";
    public static final String CHINESE_NAME_REGEX = "^[一-龥.·]+$";
    public static final String BIRTHDAY_REGEX = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
    public static final String URL_REGEX = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
    public static final String DOMAIN_REGEX = "(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)";
    public static final String POSTCODE_REGEX = "[1-9]\\d{5}";
    public static final String IPADDRESS_REGEX = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
    public static final String LETTERANDNUMBER_REGEX = "^[a-zA-Z0-9]+$";
    public static final String IP6_REGEX = "^((([0-9A-Fa-f]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2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$";
    public static final String IP4_REGEX = "^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$";

    public RegexUtil() {
    }

    public static boolean checkEmail(String email) {
        return Pattern.matches("\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?", email);
    }

    public static boolean checkIdCard(String idCard) {
        return Pattern.matches("(^[1-9]\\d{5}([12][0-9])\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)", idCard);
    }

    public static boolean checkMobile(String mobile) {
        return Pattern.matches("(\\+\\d+)?1\\d{10}$", mobile);
    }

    public static boolean checkPhone(String phone) {
        return Pattern.matches("(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$", phone);
    }

    public static boolean checkDigit(String digit) {
        return Pattern.matches("\\-?\\d+", digit);
    }

    public static boolean checkDecimals(String decimals) {
        return Pattern.matches("\\-?\\d+(\\.\\d+)?", decimals);
    }

    public static boolean checkBlankSpace(String blankSpace) {
        return Pattern.matches("\\s+", blankSpace);
    }

    public static boolean checkChinese(String chinese) {
        return Pattern.matches("^[一-龥]+$", chinese);
    }

    public static boolean checkChineseName(String chinese) {
        return Pattern.matches("^[一-龥.·]+$", chinese);
    }

    public static boolean checkBirthday(String birthday) {
        return Pattern.matches("[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}", birthday);
    }

    public static boolean checkURL(String url) {
        return Pattern.matches("(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?", url);
    }

    public static String getDomain(String url) {
        Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", 2);
        Matcher matcher = p.matcher(url);
        matcher.find();
        return matcher.group();
    }

    public static boolean checkPostcode(String postcode) {
        return Pattern.matches("[1-9]\\d{5}", postcode);
    }

    public static boolean checkIpAddress(String ipAddress) {
        return Pattern.matches("[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))", ipAddress);
    }

    public static boolean checkLetterAndNumber(String str) {
        return Pattern.matches("^[a-zA-Z0-9]+$", str);
    }

    public static boolean checkContainEmoji(String str) {
        Pattern p = Pattern.compile("[^\\u0000-\\uFFFF]", 66);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static Set<String> popMatcherValue(String regEx, String str) {
        Set<String> matcherSet = new HashSet();
        Pattern pat = Pattern.compile(regEx);
        Matcher mat = pat.matcher(str);

        while(mat.find()) {
            matcherSet.add(mat.group(1));
        }

        return matcherSet;
    }

    public static Integer checkIPVersion(String ip) {
        if (Pattern.matches("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$", ip)) {
            return 4;
        } else {
            return Pattern.matches("^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}){1,2})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){1,3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){1,4})|(([0-9A-Fa-f]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